package com.example.demo.controller;

import com.example.demo.entity.Idea;
import com.example.demo.entity.Status;
import com.example.demo.mapper.IdeaMapper;
import com.example.demo.mapper.StatusMapper;

import com.example.demo.utils.DateUtils;
import com.example.demo.utils.SavePicture;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;


import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.xml.transform.Result;
import java.io.*;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.InputStream;


@RestController
public class IdeaController {

    @Autowired
    private IdeaMapper ideaMapper;

    @Autowired
    private StatusMapper statusMapper;


    //首页的说说
    @RequestMapping("indexIdea")
    public List<Idea> indexidea(String openid){

        List<Idea> ideaList=ideaMapper.selectAll();
        for(int i=0;i<ideaList.size();i++){
            ideaList.get(i).setPictureroute("/images/"+ideaList.get(i).getPictureroute());
            int ideaid=ideaList.get(i).getIdeaid();
            List<Status> statusList=statusMapper.findByIdeaidAndOpenid(ideaid,openid);
            if(statusList.size()!=0){
                ideaList.get(i).setStatus(true);
            }else {
                ideaList.get(i).setStatus(false);
            }
        }
        return ideaList;
    }

//    //点赞说说并更改数据
//    @RequestMapping("/updatatumbupnumber")
//    public boolean updatatumbupnumber(int ideaid,String openid,int tumbupnumber){
//
//        System.out.println(ideaid+" xxxx "+openid+ "sssss"+tumbupnumber);
//
//        Status status=new Status();
//        status.setIdeaid(ideaid);
//        status.setOpenid(openid);
//        status.setStatus('a');
//        int ideanum=ideaMapper.updatacomment(tumbupnumber,ideaid);
//        int statusnum=statusMapper.InsetsStatus(status);
//
//        return true;
//    }
//
//
//    //取消点赞
//    @RequestMapping("/delectnumber")
//    public boolean delectnumber(int ideaid,String openid,int tumbupnumber){
//        Status status=new Status();
//        status.setIdeaid(ideaid);
//        status.setOpenid(openid);
//        status.setStatus('a');
//        int ideanum=ideaMapper.updatacomment(tumbupnumber,ideaid);
//        int delete=statusMapper.deleteStatus(ideaid,openid);
//        return true;
//    }



    //添加说说
    @RequestMapping("/addIdea")
    public boolean addIdea(String title,String openid,String ideacontent,String pictureroute){
     Idea idea=new Idea();
     //实例日期工具
     DateUtils dateUtils=new DateUtils();
     if(title!=null&&openid!=null&&ideacontent!=null&&pictureroute!=null){
         idea.setOpenid(openid);
         idea.setTitle(title);
         idea.setIdeacontent(ideacontent);
         idea.setIdeadate(dateUtils.getFormatDate());
         idea.setPictureroute(pictureroute);
         idea.setTumbupnumber(0);
         int i=ideaMapper.InsertIdea(idea);
         if(i==1)return true;
         else return false;
     }else {
         return false;
     }

    }

    @RequestMapping("/UploadFile")
    @ResponseBody
    public String UploadFile(MultipartFile file,HttpServletRequest request )throws IOException{

        SavePicture savePicture=new SavePicture();
        String fileName=savePicture.uploadfile(file,request);
        return fileName;
    }

}
